﻿namespace Image_Test1.ResizeClasses
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using Image_Test1.ImageClasses;
    using Image_Test1.ObjectTypes;
    using Image_Test1.StaticClasses;

    /// <summary>
    /// Bilinear Class
    /// </summary>
    public class Bilinear
    {
        /// <summary>
        /// Initializes a new instance of the Bilinear class
        /// </summary>
        public Bilinear()
        {
        }
        
        /// <summary>
        /// Resizes the image using BilinearResize
        /// </summary>
        /// <param name="newSize">Size of the new Image</param>
        public Bitmap ResizeImage(Size newSize, Bitmap ImageFile, Color[,] Buffer)
        {
            
            int NewWidth = newSize.Width, NewHigth = newSize.Height;
            int OldWidth = ImageFile.Width, OldHigth = ImageFile.Height;
            float OldX, OldY, NewX, NewY;

            float WRatio = ((float)OldWidth / NewWidth), HRatio = ((float)OldHigth / NewHigth);
            int X1, X2, Y1, Y2;
            Color P1, P2, P3, P4;
            float XFraction, YFraction;
            float Z1, Z2;

            Image_Test1.MyColor NewPixel = new Image_Test1.MyColor();
            Bitmap NewBu = new Bitmap(NewWidth, NewHigth);
            for (int i = 0; i < NewWidth; i++)
            {
                NewX = i;
                for (int j = 0; j < NewHigth; j++)
                {
                    NewY = j;
                    OldX = NewX * WRatio;
                    OldY = NewY * HRatio;
                    X1 = (int)Math.Floor(OldX);
                    X2 = X1 + 1;
                    Y1 = (int)Math.Floor(OldY);
                    Y2 = Y1 + 1;
                    if (X2 == OldWidth) X2 -= 1;
                    if (Y2 == OldHigth) Y2 -= 1;
                    P1 = Buffer[X1, Y1]; P2 = Buffer[X2, Y1];
                    P3 = Buffer[X1, Y2]; P4 = Buffer[X2, Y2];
                    XFraction = OldX - X1;
                    YFraction = OldY - Y1;

                    Z1 = (float)(P1.R * (1 - XFraction) + P2.R * XFraction);
                    Z2 = (float)(P3.R * (1 - XFraction) + P4.R * XFraction);
                    NewPixel.Red = (int)(Z1 * (1 - YFraction) + Z2 * YFraction);

                    Z1 = (float)(P1.G * (1 - XFraction) + P2.G * XFraction);
                    Z2 = (float)(P3.G * (1 - XFraction) + P4.G * XFraction);
                    NewPixel.Green = (int)(Z1 * (1 - YFraction) + Z2 * YFraction);

                    Z1 = (float)(P1.B * (1 - XFraction) + P2.B * XFraction);
                    Z2 = (float)(P3.B * (1 - XFraction) + P4.B * XFraction);
                    NewPixel.Blue = (int)(Z1 * (1 - YFraction) + Z2 * YFraction);

                    //NewBuffer[(int)NewX, (int)NewY] = NewPixel;
                    NewBu.SetPixel((int)NewX, (int)NewY, Color.FromArgb(255, NewPixel.Red, NewPixel.Green, NewPixel.Blue));
                }
            }
            return NewBu;
            //DisplayPicture2.Image = NewBu;
            //ImageInfo2.Text = NewWidth.ToString() + " Width - " + NewHigth.ToString() + " Higth";
        }

        /// <summary>
        /// Creats a Shallow Copy of this Object
        /// </summary>
        /// <returns>Shallow Copy</returns>
    }

}
